package com.opennews.openplatform.myspringbootcore.helper;

import lombok.RequiredArgsConstructor;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@Component
public class AMapHelper {
    private final Environment env;
    private final HttpRequestHelper httpRequestHelper;

    private final String version = "v3";
    private final String successStatus = "1";

    /**
     * Queries location by coordinate.
     *
     * @param latitude
     * @param longitude
     */
    public Map<String, Object> queryByCoordinate(double latitude, double longitude) {
        try {
            Map<String, Object> response = httpRequestHelper.get(env.getProperty("amap.restApiUrl"), String.format("%s/geocode/regeo", version), Map.of(), Map.of(
                    "key", env.getProperty("amap.webApiKey"),
                    "location", String.format("%s,%s", longitude, latitude)
            ));

            if (successStatus.equals(response.get("status"))) {
                Map<String, Object> regeocode = (Map<String, Object>) response.get("regeocode");
                Map<String, Object> addressComponent = (Map<String, Object>) regeocode.get("addressComponent");

                Map<String, Object> locationDetails = new HashMap<>();
                locationDetails.put("latitude", latitude);
                locationDetails.put("longitude", longitude);
                locationDetails.put("country", getStringValue(addressComponent.get("country")));
                locationDetails.put("province", getStringValue(addressComponent.get("province")));
                locationDetails.put("city", getStringValue(addressComponent.get("city")));
                locationDetails.put("district", getStringValue(addressComponent.get("district")));
                locationDetails.put("township", getStringValue(addressComponent.get("township")));
                locationDetails.put("formattedAddress", getStringValue(regeocode.get("formatted_address")));
                locationDetails.put("adCode", getStringValue(addressComponent.get("adcode")));
                locationDetails.put("cityCode", getStringValue(addressComponent.get("citycode")));
                locationDetails.put("townCode", getStringValue(addressComponent.get("towncode")));

                return locationDetails;
            }
        } catch (Exception ex) {
            return null;
        }

        return null;
    }

    /**
     * Queries poi by keywords.
     * @param keywords
     */
    public List<Map<String, Object>> queryPoiByKeywords(String keywords) {
        try {
            Map<String, Object> response = httpRequestHelper.get(env.getProperty("amap.restApiUrl"), String.format("%s/place/text", version), Map.of(), Map.of(
                    "key", env.getProperty("amap.webApiKey"),
                    "keywords", keywords
            ));

            if (successStatus.equals(response.get("status"))) {
                return (List<Map<String, Object>>) response.get("pois");
            }
        } catch (Exception ex) {
            return null;
        }

        return null;
    }

    /**
     * If coordinate is outside of China item of response will be an empty list instead of string.
     * That's why we need to check on it.
     * @param item
     * @return
     */
    private String getStringValue(Object item) {
        return item instanceof List ? "" : item.toString();
    }
}
